package com.lvy.hczn.front.business.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.system.domain.business.PurchaseOrder;
import com.lvy.hczn.front.system.domain.business.PurchaseOrderDetail;
import com.lvy.hczn.front.system.domain.business.Spu;
import com.lvy.hczn.front.business.dto.Param;
import com.lvy.hczn.front.business.service.PurchaseOrderDetailService;
import com.lvy.hczn.front.business.service.PurchaseOrderService;
import com.lvy.hczn.front.system.service.SpuService;
import com.lvy.hczn.front.system.service.WarehouseService;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.core.domain.BaseModel;
import com.lvy.hczn.front.common.exception.UtilException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/wcs/order/entry/")
@RestController
@Slf4j
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private PurchaseOrderDetailService purchaseOrderDetailService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private SpuService spuService;

    @ApiOperation(value = "分拣加工任务", notes = "分拣加工任务")
    @PostMapping("selectByCode")
    public ResultUtil selectByCode(@RequestBody Param param) {
        try {
            return ResultUtil.ok();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    @ApiOperation(value = "生成采购单及详情", notes = "分拣加工任务")
    @PostMapping("generatePurchaseOrder")
    public ResultUtil generatePurchaseOrder(@RequestBody Map<String, Object> map) {
        try {
            String warehouseCode = warehouseService.selectLocalWarehouseFront().getWarehouseCode();
//            List<Spu> spuList = spuService.list();
            List<Spu> spuList = new ArrayList<>();
            if (map != null && map.containsKey("ids")) {
                List<String> ids = (List<String>) map.get("ids");
                spuList = spuService.list(Wrappers.<Spu>lambdaQuery().in(BaseModel::getId, ids));
            } else {
                spuList = spuService.list();
            }
            List<String> unitList = new ArrayList<>();
//            unitList.add("箱");
//            unitList.add("袋");
//            unitList.add("盒");
//            unitList.add("斤");
            unitList.add("件");
            //箱数
            int num = 1;
            //采购数量(瓶数)
            BigDecimal purchaseNum = BigDecimal.valueOf(7);
            if (map != null) {
                if (map.containsKey("num")) {
                    num = (Integer) map.get("num");
                }
                if (map.containsKey("purchaseNum")) {
                    purchaseNum = new BigDecimal(map.get("purchaseNum").toString());
                }
            }
            for (int i = 0; i < 1; i++) {
                PurchaseOrder purchaseOrder = new PurchaseOrder();
                purchaseOrder.setPurchaseOrderCode("CGD" + IdUtil.getSnowflake().nextIdStr());
                purchaseOrder.setWarehouseCode(warehouseCode);
                purchaseOrder.setPurchaserId(warehouseCode);
                purchaseOrder.setSupplierCode(warehouseCode);
                purchaseOrder.setDeliveryWay("2");
                purchaseOrder.setPurchaseSource("0");
                purchaseOrder.setConfirmStatus("1");
                purchaseOrder.setDeliveryTime(LocalDateTime.now());
                purchaseOrder.setDeliveryStatus("0");
                purchaseOrder.setDistributionCost(BigDecimal.TEN);
                purchaseOrder.setPrice(BigDecimal.TEN);
                purchaseOrderService.save(purchaseOrder);

                for (int j = 0; j < spuList.size(); j++) {
                    Spu spu = spuList.get(j);
                    PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
//                    int index = RandomUtil.randomInt(1, spuList.size());
                    purchaseOrderDetail.setSpuId(spuList.get(j).getId());
                    purchaseOrderDetail.setPurchaseOrderCode(purchaseOrder.getPurchaseOrderCode());
                    purchaseOrderDetail.setBatchCode(IdUtil.getSnowflake().nextIdStr());
                    purchaseOrderDetail.setSpecCode(IdUtil.getSnowflake().nextIdStr());
                    purchaseOrderDetail.setPrice(BigDecimal.TEN);
                    if (spu.getId().equals("43")) {
                        //今麦郎0.57
                        weight(BigDecimal.valueOf(0.57), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("44")) {
                        //今麦郎0.57
                        weight(BigDecimal.valueOf(0.57), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("45")) {
                        //百事可乐0.32
                        weight(BigDecimal.valueOf(0.32), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("46")) {
                        //七喜(西柚味)0.59
                        weight(BigDecimal.valueOf(0.59), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("47")) {
                        //七喜（柠檬味）0.59
                        weight(BigDecimal.valueOf(0.59), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("48")) {
                        //七喜（柠檬味）1.07
                        weight(BigDecimal.valueOf(1.07), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("50")) {
                        //百事可乐（无糖）0.53
                        weight(BigDecimal.valueOf(0.53), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("51")) {
                        //百事可乐苏打气泡水0.48
                        weight(BigDecimal.valueOf(0.48), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("52")) {
                        //佳得乐（西柚味）0.65
                        weight(BigDecimal.valueOf(0.65), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("53")) {
                        //佳得乐（奇异果味）0.65
                        weight(BigDecimal.valueOf(0.65), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("54")) {
                        //微笑趣泡（百香果）0.48
                        weight(BigDecimal.valueOf(0.48), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("55")) {
                        //微笑趣泡（白桃）0.48
                        weight(BigDecimal.valueOf(0.48), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("57")) {
                        //美年达（百香果菠萝味）0.53
                        weight(BigDecimal.valueOf(0.53), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("58")) {
                        //岳麓湘乡（中）0.54
                        weight(BigDecimal.valueOf(0.54), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("59")) {
                        //岳麓湘乡（小）0.52
                        weight(BigDecimal.valueOf(0.52), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("23")) {
                        //岳麓湘乡（小）0.54
                        weight(BigDecimal.valueOf(0.54), purchaseNum, purchaseOrderDetail);
                    } else if (spu.getId().equals("25")) {
                        //岳麓湘乡（小）0.54
                        weight(BigDecimal.valueOf(0.54), purchaseNum, purchaseOrderDetail);
                    } else {
                        //0.57
                        weight(BigDecimal.valueOf(0.57), purchaseNum, purchaseOrderDetail);
                    }

                    purchaseOrderDetail.setQuantity(num);
                    purchaseOrderDetail.setPurchaseQuantity(num);
                    purchaseOrderDetail.setUnit(unitList.get(RandomUtil.randomInt(unitList.size())));
                    purchaseOrderDetail.setStatus("0");
                    purchaseOrderDetailService.save(purchaseOrderDetail);
                }
            }
            return ResultUtil.ok();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    public void weight(BigDecimal perWeight, BigDecimal purchaseNum, PurchaseOrderDetail purchaseOrderDetail) {
        purchaseOrderDetail.setNumberOfPerBox(purchaseNum.intValue());
        purchaseOrderDetail.setWeightOfPerBox(perWeight);
        BigDecimal purchaseWeight = perWeight.multiply(purchaseNum).setScale(2, RoundingMode.HALF_UP);
        purchaseOrderDetail.setPurchaseWeight(purchaseWeight);
        purchaseOrderDetail.setWeight(purchaseWeight);
    }

    @ApiOperation(value = "采购入库判断是否有正在入库", notes = "采购入库判断是否有正在入库")
    @PostMapping("checkPurchaseEntry")
    public ResultUtil checkPurchaseEntry(@RequestBody Param param) {
        try {
            return purchaseOrderService.checkPurchaseEntry(param);
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }
}
